/////////////////////////////////////////////////////////////////////////////
// Name:        mainframe.cpp
// Purpose:     
// Author:      Julian Smart
// Modified by: 
// Created:     22/03/2009 10:28:01
// RCS-ID:      
// Copyright:   
// Licence:     
/////////////////////////////////////////////////////////////////////////////

#include "wx/wx.h"

#include "ax/ax_containerctrl.h"
#include "ax/ax_controller_standard.h"
#include "ax/ax_view.h"
#include "ax/ax_menu_folder.h"
#include "ax/ax_menu_choice.h"
#include "ax/ax_menu_toggle.h"
#include "ax/ax_menu_text.h"
#include "ax/ax_menu_range.h"
#include "ax/ax_menu_speech_settings.h"
#include "ax/ax_menu_visual_settings.h"
#include "ax/ax_menu_shortcuts.h"
#include "ax/ax_messagebox.h"

////@begin includes
////@end includes

#include "mainframe.h"
#include "axsampleapp.h"
#include "samplesettingsdialog.h"
#include "sampledialog.h"

////@begin XPM images
#include "ax.xpm"
////@end XPM images

/*!
 * SampleMainFrame type definition
 */

IMPLEMENT_CLASS( SampleMainFrame, wxFrame )


/*!
 * SampleMainFrame event table definition
 */

BEGIN_EVENT_TABLE( SampleMainFrame, wxFrame )

    EVT_MENU( AxGetId(AxIdentifierStopSpeech),      SampleMainFrame::OnStopSpeechClick )
    EVT_MENU( AxGetId(AxIdentifierGoHome),          SampleMainFrame::OnGoHomeClick )
    EVT_MENU( AxGetId(AxIdentifierGoUp),            SampleMainFrame::OnGoUpClick )
    EVT_MENU( AxGetId(AxIdentifierDescribeContext), SampleMainFrame::OnDescribeContextClick )
    EVT_MENU( AxGetId(AxIdentifierDescribeItem),    SampleMainFrame::OnDescribeItemClick )

////@begin SampleMainFrame event table entries
    EVT_CLOSE( SampleMainFrame::OnCloseWindow )

    EVT_MENU( wxID_PREFERENCES, SampleMainFrame::OnPreferencesClick )

    EVT_MENU( wxID_EXIT, SampleMainFrame::OnExitClick )

    EVT_MENU( ID_TEST_DIALOG, SampleMainFrame::OnTestDialogClick )

    EVT_MENU( ID_MESSAGE_DIALOG, SampleMainFrame::OnMessageDialogClick )

    EVT_MENU( ID_LOG_DIALOG, SampleMainFrame::OnLogDialogClick )

    EVT_BUTTON( ID_STOP_SPEECH, SampleMainFrame::OnStopSpeechClick )
    EVT_UPDATE_UI( ID_STOP_SPEECH, SampleMainFrame::OnStopSpeechUpdate )

    EVT_BUTTON( ID_GO_HOME, SampleMainFrame::OnGoHomeClick )
    EVT_UPDATE_UI( ID_GO_HOME, SampleMainFrame::OnGoHomeUpdate )

    EVT_BUTTON( ID_GO_UP, SampleMainFrame::OnGoUpClick )
    EVT_UPDATE_UI( ID_GO_UP, SampleMainFrame::OnGoUpUpdate )

    EVT_BUTTON( ID_DESCRIBE_CONTEXT, SampleMainFrame::OnDescribeContextClick )

    EVT_BUTTON( ID_DESCRIBE_ITEM, SampleMainFrame::OnDescribeItemClick )

////@end SampleMainFrame event table entries

END_EVENT_TABLE()


/*!
 * SampleMainFrame constructors
 */

SampleMainFrame::SampleMainFrame()
{
    Init();
}

SampleMainFrame::SampleMainFrame( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
    Init();
    Create( parent, id, caption, pos, size, style );
}


/*!
 * SampleMainFrame creator
 */

bool SampleMainFrame::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
////@begin SampleMainFrame creation
    wxFrame::Create( parent, id, caption, pos, size, style );

    CreateControls();
    SetIcon(GetIconResource(wxT("ax.xpm")));
    Centre();
////@end SampleMainFrame creation
    return true;
}


/*!
 * SampleMainFrame destructor
 */

SampleMainFrame::~SampleMainFrame()
{
////@begin SampleMainFrame destruction
////@end SampleMainFrame destruction
}


/*!
 * Member initialisation
 */

void SampleMainFrame::Init()
{
////@begin SampleMainFrame member initialisation
    m_containerCtrl = NULL;
////@end SampleMainFrame member initialisation
}


/*!
 * Control creation for SampleMainFrame
 */

void SampleMainFrame::CreateControls()
{    
////@begin SampleMainFrame content construction
    SampleMainFrame* itemFrame1 = this;

    wxMenuBar* menuBar = new wxMenuBar;
    wxMenu* itemMenu3 = new wxMenu;
    itemMenu3->Append(wxID_PREFERENCES, _("&Preferences...\tCtrl+T"), wxEmptyString, wxITEM_NORMAL);
    itemMenu3->Append(ID_GO_UP, _("Go &Up"), wxEmptyString, wxITEM_NORMAL);
    itemMenu3->AppendSeparator();
    itemMenu3->Append(wxID_EXIT, _("&Exit"), wxEmptyString, wxITEM_NORMAL);
    menuBar->Append(itemMenu3, _("&File"));
    wxMenu* itemMenu8 = new wxMenu;
    itemMenu8->Append(ID_GO_UP, _("Go &Up"), wxEmptyString, wxITEM_NORMAL);
    itemMenu8->Append(ID_GO_HOME, _("Go &Home"), wxEmptyString, wxITEM_NORMAL);
    itemMenu8->AppendSeparator();
    itemMenu8->Append(ID_STOP_SPEECH, _("&Stop Speech"), wxEmptyString, wxITEM_NORMAL);
    itemMenu8->AppendSeparator();
    itemMenu8->Append(ID_DESCRIBE_CONTEXT, _("Describe &Context"), wxEmptyString, wxITEM_NORMAL);
    itemMenu8->Append(ID_DESCRIBE_ITEM, _("Describe I&tem"), wxEmptyString, wxITEM_NORMAL);
    menuBar->Append(itemMenu8, _("&View"));
    wxMenu* itemMenu16 = new wxMenu;
    itemMenu16->Append(ID_TEST_DIALOG, _("Test &Voiced Dialog"), wxEmptyString, wxITEM_NORMAL);
    itemMenu16->Append(ID_MESSAGE_DIALOG, _("Test &Message Dialog"), wxEmptyString, wxITEM_NORMAL);
    itemMenu16->Append(ID_LOG_DIALOG, _("Test &Log Dialog"), wxEmptyString, wxITEM_NORMAL);
    menuBar->Append(itemMenu16, _("&Tests"));
    itemFrame1->SetMenuBar(menuBar);

    wxPanel* itemPanel20 = new wxPanel( itemFrame1, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxNO_BORDER|wxTAB_TRAVERSAL );

    wxBoxSizer* itemBoxSizer21 = new wxBoxSizer(wxVERTICAL);
    itemPanel20->SetSizer(itemBoxSizer21);

    wxBoxSizer* itemBoxSizer22 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer21->Add(itemBoxSizer22, 0, wxGROW, 5);

    wxButton* itemButton23 = new wxButton( itemPanel20, ID_STOP_SPEECH, _("Stop Speech"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer22->Add(itemButton23, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxButton* itemButton24 = new wxButton( itemPanel20, ID_GO_HOME, _("Go Home"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer22->Add(itemButton24, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxButton* itemButton25 = new wxButton( itemPanel20, ID_GO_UP, _("Go Up"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer22->Add(itemButton25, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxButton* itemButton26 = new wxButton( itemPanel20, ID_DESCRIBE_CONTEXT, _("Describe Context"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer22->Add(itemButton26, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxButton* itemButton27 = new wxButton( itemPanel20, ID_DESCRIBE_ITEM, _("Describe Item"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer22->Add(itemButton27, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    m_containerCtrl = new AxContainerCtrl( itemPanel20, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxNO_BORDER|wxTAB_TRAVERSAL );
    itemBoxSizer21->Add(m_containerCtrl, 1, wxGROW, 5);

////@end SampleMainFrame content construction

    // Update the menubar accelerators from the shortcut manager
    wxGetApp().GetShortcutManager().UpdateMenuBar(GetMenuBar());

    wxGetApp().GetViewManager().SetContainerCtrl(m_containerCtrl);
    wxGetApp().GetViewManager().SetViewPagerCtrl(m_containerCtrl->GetPagerCtrl());
    wxGetApp().GetViewManager().SetTitleCtrl(m_containerCtrl->GetTitleCtrl());
    wxGetApp().GetViewManager().SetDescriptionCtrl(m_containerCtrl->GetDescriptionCtrl());
    wxGetApp().GetViewManager().SetMenuCtrl(m_containerCtrl->GetMenuCtrl());

    wxString homeDescription = _("Welcome to the Accessibility Framework basic sample!\n\n\
Browse the application using the simple menu interface.\n\n\
The idea behind the framework is to provide a consistent UI for the user and reusable API for the developer.");

    wxGetApp().GetViewManager().SetHomeDescription(homeDescription);

    wxGetApp().GetViewManager().SetShortcutManager(& wxGetApp().GetShortcutManager());

    AxStandardMenuController* controller =
        new AxStandardMenuController(m_containerCtrl->GetMenuCtrl(), m_containerCtrl->GetTitleCtrl());

    wxGetApp().GetViewManager().SetMenuController(controller);
    controller->SetViewManager(& wxGetApp().GetViewManager());

    m_containerCtrl->SetController(controller);
    m_containerCtrl->GetMenuCtrl()->SetController(controller);

    m_adapter.Adapt(this);

    AxMenu* menu = new AxMenu(_("Main Menu"), _("This is the main menu."));
    AxMenuItem* menuItem = NULL;
    {
        menuItem = new AxMenuItem(_("Menu item tests"), _("Test a variety of menu item types from here."));
        AxMenu* controlsMenu = new AxMenu(_("Menu item tests"), _("Test a variety of menu item types from here."));
        menuItem->SetSubMenu(controlsMenu);
        menu->AddItem(menuItem);
        
        controlsMenu->AddItem(new AxMenuItem(_("Test modal choice menu"), _("Test the modal choice menu"),
            wxEmptyString, new TestChoiceActivator()));
        
        wxArrayString labels, ids, descriptions;
        
        labels.Add(_("This is the first choice"));
        labels.Add(_("Another choice"));
        labels.Add(_("And a third choice"));
        
        ids.Add(wxT("ONE"));
        ids.Add(wxT("TWO"));
        ids.Add(wxT("THREE"));
        
        descriptions.Add(_("Please select this choice if you wish."));
        descriptions.Add(_("The second choice is also up for grabs."));
        descriptions.Add(_("Select the final choice if you feel it is appropriate."));
        
        wxGetApp().m_testString = _("Another choice.");
        
        menuItem = new AxChoiceMenuItem(_("Test the choice menu item: %VALUE%"),
            _("Test AxChoiceMenuItem. The current value is %VALUE%."),
            wxEmptyString, labels, ids, descriptions);
        menuItem->SetValidator(new AxChoiceValidator(& wxGetApp().m_testString));
        
        controlsMenu->AddItem(menuItem);
        
        menuItem = new AxFolderMenuItem(_("Test the folder menu item: %FOLDERNAME%"),
            _("Test AxFolderMenuItem. The current value is %FOLDER%"),
            wxEmptyString,
            wxGetApp().m_testFolderString,
            _("*"));
        menuItem->SetValidator(new AxFolderValidator(& wxGetApp().m_testFolderString));
        
        controlsMenu->AddItem(menuItem);
        
        menuItem = new AxFileMenuItem(_("Test the file menu item: %FOLDERNAME%"),
            _("Test AxFileMenuItem. The current value is %FOLDER%"),
            wxEmptyString,
            wxGetApp().m_testFileString,
            _("*"));
        menuItem->SetValidator(new AxFolderValidator(& wxGetApp().m_testFileString));
        
        controlsMenu->AddItem(menuItem);
        
        menuItem = new AxTextMenuItem(
            _("Test the text menu item: %VALUE%"),
            _("Test AxTextMenuItem. Please enter a value. Currently, the value is %VALUE%"),
            wxEmptyString,
            _("Enter a string"),
            _("This is just a test. Please enter a string."),
            wxGetApp().m_testString);
        menuItem->SetValidator(new AxTextValidator(& wxGetApp().m_testString));
        
        controlsMenu->AddItem(menuItem);
        
        menuItem = new AxTextMenuItem(
            _("Test the multiline text menu item: %VALUE%"),
            _("Test AxTextMenuItem in multiline mode. Please enter a value. Currently, the value is %VALUE%"),
            wxEmptyString,
            _("Enter a string"),
            _("This is just a test. Please enter a string."),
            wxGetApp().m_testString,
            axMENU_STYLE_MULTILINE_TEXT);
        menuItem->SetValidator(new AxTextValidator(& wxGetApp().m_testString));
        
        controlsMenu->AddItem(menuItem);
        
        menuItem = new AxToggleMenuItem(
            _("Test the toggle menu item (is off)"), 
            _("Test the toggle menu item (is on)"), 
            _("This is a toggle item, called AxToggleMenuItem."),
            wxEmptyString, wxGetApp().m_toggle);
        menuItem->SetValidator(new AxToggleValidator(& wxGetApp().m_toggle));
        controlsMenu->AddItem(menuItem);
        
        menuItem = new AxRangeMenuItem(_("Test the range menu item (is %VALUE%)"),
            _("This is a range item, called AxRangeMenuItem."),
            wxEmptyString, 1, 50, wxGetApp().m_testRangeValue);
        menuItem->SetValidator(new AxRangeValidator(& wxGetApp().m_testRangeValue));
        controlsMenu->AddItem(menuItem);
        
        menuItem = new AxMenuItem(_("Disabled item"), _("This is a disabled item"));
        menuItem->SetActivator(new AxEventActivator(menuItem, this, wxID_EXIT));
        menuItem->Enable(false);
        controlsMenu->AddItem(menuItem);

        controlsMenu->AddItem(new AxGoUpMenuItem);
    }

    
    {
        menuItem = new AxMenuItem(_("Dialog tests"), _("Test a variety of dialogs from here."));
        AxMenu* dialogsMenu = new AxMenu(_("Dialog tests"), _("Test a variety of dialogs from here."));
        menuItem->SetSubMenu(dialogsMenu);
        menu->AddItem(menuItem);

        menuItem = new AxMenuItem(_("Test a self-voiced dialog"), _("Shows how a dialog can be made self-voiced."));
        menuItem->SetActivator(new AxEventActivator(menuItem, this, ID_TEST_DIALOG));
        dialogsMenu->AddItem(menuItem);

        menuItem = new AxMenuItem(_("Test a log dialog"), _("Demonstrates the AxLog class."));
        menuItem->SetActivator(new AxEventActivator(menuItem, this, ID_LOG_DIALOG));
        dialogsMenu->AddItem(menuItem);

        menuItem = new AxMenuItem(_("Test the message box dialog"), _("Demonstrates the AxMessageDialog class."));
        menuItem->SetActivator(new AxEventActivator(menuItem, this, ID_MESSAGE_DIALOG));
        dialogsMenu->AddItem(menuItem);

        dialogsMenu->AddItem(new AxGoUpMenuItem);
    }

    menu->AddItem(new AxSpeechSettingsMenuItem(controller, & wxGetApp().GetViewManager().GetSpeech()));

    menu->AddItem(new AxVisualSettingsMenuItem(controller, & wxGetApp().GetViewManager().GetSpeech()));

    menu->AddItem(new AxShortcutsMenuItem(controller,
        & wxGetApp().GetViewManager().GetSpeech(),
        & wxGetApp().GetShortcutManager(),
        this /* the window to set the shortcuts in */,
        GetMenuBar()));

    menuItem = new AxMenuItem(_("Exit"), _("Quit the program"));
    menuItem->SetActivator(new AxEventActivator(menuItem, this, wxID_EXIT));
    menuItem->SetStyle(AxMenuStyleBold);
    menu->AddItem(menuItem);

    // Make sure that data and display are consistent.
    menu->TransferDataToMenu();

    wxGetApp().GetViewManager().ShowInitialMenu(menu);
    wxGetApp().GetViewManager().ShowHomeView();

    //wxGetApp().GetShortcutManager().SetAcceleratorTable(this);
}

/*!
 * Should we show tooltips?
 */

bool SampleMainFrame::ShowToolTips()
{
    return true;
}

/*!
 * Get bitmap resources
 */

wxBitmap SampleMainFrame::GetBitmapResource( const wxString& name )
{
    // Bitmap retrieval
////@begin SampleMainFrame bitmap retrieval
    wxUnusedVar(name);
    return wxNullBitmap;
////@end SampleMainFrame bitmap retrieval
}

/*!
 * Get icon resources
 */

wxIcon SampleMainFrame::GetIconResource( const wxString& name )
{
    // Icon retrieval
////@begin SampleMainFrame icon retrieval
    wxUnusedVar(name);
    if (name == _T("ax.xpm"))
    {
        wxIcon icon(ax_xpm);
        return icon;
    }
    return wxNullIcon;
////@end SampleMainFrame icon retrieval
}


/*!
 * wxEVT_COMMAND_MENU_SELECTED event handler for wxID_EXIT
 */

void SampleMainFrame::OnExitClick( wxCommandEvent& WXUNUSED(event) )
{
    Close();
}


/*!
 * wxEVT_COMMAND_MENU_SELECTED event handler for wxID_PREFERENCES
 */

void SampleMainFrame::OnPreferencesClick( wxCommandEvent& WXUNUSED(event) )
{
    SampleSettingsDialog dialog(this);
    dialog.ShowModal();
}


/*!
 * wxEVT_CLOSE_WINDOW event handler for ID_SAMPLEMAINFRAME
 */

void SampleMainFrame::OnCloseWindow( wxCloseEvent& event )
{
    if (wxGetApp().GetViewManager().IsModal() && event.CanVeto())
    {
        AxMessageBox(_("Sorry, cannot quit because a menu choice needs to be made."));
        event.Veto();
        return;
    }
    else
    {
        if (IsMaximized())
            wxGetApp().m_frameStatus = SHOW_STATUS_MAXIMIZED ;
        else if (IsIconized())
            wxGetApp().m_frameStatus = SHOW_STATUS_MINIMIZED ;
        else
            wxGetApp().m_frameStatus = SHOW_STATUS_NORMAL ;
        
        if (!IsIconized() && !IsMaximized())
            wxGetApp().m_frameRect = GetRect();

        Show(false);
        wxGetApp().GetViewManager().ClearViews();
        Destroy();
    }
}


/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_STOP_SPEECH
 */

void SampleMainFrame::OnStopSpeechClick( wxCommandEvent& WXUNUSED(event) )
{
    if (wxGetApp().GetViewManager().CanStopSpeech())
        wxGetApp().GetViewManager().StopSpeech();
}


/*!
 * wxEVT_UPDATE_UI event handler for ID_STOP_SPEECH
 */

void SampleMainFrame::OnStopSpeechUpdate( wxUpdateUIEvent& event )
{
    event.Enable(wxGetApp().GetViewManager().CanStopSpeech());
}


/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_GO_HOME
 */

void SampleMainFrame::OnGoHomeClick( wxCommandEvent& WXUNUSED(event) )
{
    if (wxGetApp().GetViewManager().GetMenuController()->CanGoHome())
        wxGetApp().GetViewManager().GetMenuController()->GoHome();
}


/*!
 * wxEVT_UPDATE_UI event handler for ID_GO_HOME
 */

void SampleMainFrame::OnGoHomeUpdate( wxUpdateUIEvent& event )
{
    event.Enable(wxGetApp().GetViewManager().GetMenuController()->CanGoHome());
}


/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_GO_UP
 */

void SampleMainFrame::OnGoUpClick( wxCommandEvent& WXUNUSED(event) )
{
    if (wxGetApp().GetViewManager().GetMenuController()->CanGoUp())
        wxGetApp().GetViewManager().GetMenuController()->GoUp();
}


/*!
 * wxEVT_UPDATE_UI event handler for ID_GO_UP
 */

void SampleMainFrame::OnGoUpUpdate( wxUpdateUIEvent& event )
{
    event.Enable(wxGetApp().GetViewManager().GetMenuController()->CanGoUp());
}


/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_DESCRIBE_CONTEXT
 */

void SampleMainFrame::OnDescribeContextClick( wxCommandEvent& WXUNUSED(event) )
{
    wxGetApp().GetViewManager().GetMenuController()->DescribeCurrentContext();
}


/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_DESCRIBE_ITEM
 */

void SampleMainFrame::OnDescribeItemClick( wxCommandEvent& WXUNUSED(event) )
{
    wxGetApp().GetViewManager().GetMenuController()->DescribeCurrentItem();
}


/*!
 * wxEVT_COMMAND_MENU_SELECTED event handler for ID_TEST_DIALOG
 */

void SampleMainFrame::OnTestDialogClick( wxCommandEvent& WXUNUSED(event) )
{
    SampleDialog dialog(this);
    dialog.ShowModal();
}


/*!
 * wxEVT_COMMAND_MENU_SELECTED event handler for ID_MESSAGE_DIALOG
 */

void SampleMainFrame::OnMessageDialogClick( wxCommandEvent& WXUNUSED(event) )
{
    AxMessageBox(_("This is an example of the message dialog, that works in much the same way as the standard message box."), _("Testing Message Dialog"),
        wxYES_NO|wxOK|wxCANCEL|wxNO_TO_ALL);
}


/*!
 * wxEVT_COMMAND_MENU_SELECTED event handler for ID_LOG_DIALOG
 */

void SampleMainFrame::OnLogDialogClick( wxCommandEvent& WXUNUSED(event) )
{
    wxLogError(_("This is a dummy error. Everything has gone horribly wrong."));
    wxLogError(_("And an additional error. Only a small thing has happened this time."));
}

